$(function() {
	
	$("#outSendRegister").click(function(){
		if (confirm("您真的要退出注册吗?")) {
			window.location.href = "/onlinehotel/index.jsp";
		}
	});

	$("#senderAccount").blur(function() {
		var senderAccount = $("#senderAccount").val();
		checksenderAccount(senderAccount)
	});
	
	$("#senderPwd").blur(function() {
		var senderPwd = $("#senderPwd").val();
		checksenderPwd(senderPwd)
	});
	
	$("#senderPwd2").blur(function() {
		var senderPwd = $("#senderPwd").val();
		var senderPwd2 = $("#senderPwd2").val();
		checksenderPwd2(senderPwd, senderPwd2)
	});
	
	$("#senderEmail").blur(function() {
		var senderEmail = $("#senderEmail").val();
		checksenderEmail(senderEmail)
	});
	
	$("#senderPhone").blur(function() {
		var senderPhone = $("#senderPhone").val();
		checksenderPhone(senderPhone)
	});
	
	$("#senderAccount").focus(function() {
		senderAccountSpan.innerHTML = "<font size=2 color=red>·为了方便记忆,您可用手机号注册!</font>";
	});
	
	$("#senderEmail").focus(function() {
		senderEmailSpan.innerHTML = "<font size=2 color=red>·邮箱的格式为：xxx@xxx.xxx</font>";
	});
	
	$("#btnSender").click(function(){
		var flag = true;
		checksenderAccount($("#senderAccount").val());
		if (!checksenderPwd($("#senderPwd").val() )) {
			flag = false;
		}
		if (!checksenderPwd2($("#senderPwd").val(),$("#senderPwd2").val() )) {
			flag = false;
		}
		if (!checksenderEmail($("#senderEmail").val())) {
			flag = false;
		}
		if (!checksenderPhone($("#senderPhone").val())) {
			flag = false;
		} 
		
		if(flag){ 
			var senderAccount = $("#senderAccount").val();
			var zz = /^\w{4,18}$/
			if (zz.test(senderAccount)){
				$.ajax({
				type : "post",
				url : "sender.do",
				data : "method=isExist&senderAccount="+senderAccount,
				cache : false,
				success:function(msg){			
					if("true"==msg){
						senderAccountSpan.innerHTML = "<font size=2 color=red>账号已使用</font>";
					}else{
						senderAccountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";				
						$("#sender").submit();
					}
				}
				});		
			} else {
				senderAccountSpan.innerHTML = "<font size=2 color=red>·请输入4-18个字符,包括字母、数字、下划线!</font>";
			}
		}
	});
});

function checksenderAccount(senderAccount){
	var zz = /^\w{4,18}$/
	if (zz.test(senderAccount)){
		alert(senderAccount);
		$.ajax({
		type : "post",
		url : "sender.do",
		data : "method=isExist&senderAccount="+senderAccount,
		cache : false,
		success:function(msg){
			
			if("true"==msg){
				senderAccountSpan.innerHTML = "<font size=2 color=red>账号已使用</font>";
				return false;
			}else{
				senderAccountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
				var z = /^1[3|4|5|8][0-9]\d{8}$/
				if(z.test(senderAccount)){
					var senderPhone = document.getElementById("senderPhone");
					senderPhone.value = senderAccount;
					senderPhoneSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
				}
				return true;
			}
		}
		});		
	} else {
		senderAccountSpan.innerHTML = "<font size=2 color=red>·请输入4-18个字符,包括字母、数字、下划线!</font>";
		return false;
	}
}

function checksenderPwd(senderPwd){
	var zz = /^\w{4,16}$/
	if (zz.test(senderPwd)) {
		senderPwdSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		senderPwdSpan.innerHTML = "<font size=2 color=red>·请输入4-16个字符,包括字母、数字、下划线!</font>";
		return false;
	}
}

function checksenderPwd2(senderPwd,senderPwd2){
	if (senderPwd != senderPwd2) {
		senderPwdSpan2.innerHTML = "<font size=2 color=red>·对不起,两次密码不一致!</font>";
		return false;
	} else {
		senderPwdSpan2.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	}
}

function checksenderEmail(senderEmail){
	
	var zz = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
	if (zz.test(senderEmail)) {
		senderEmailSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		senderEmailSpan.innerHTML = "<font size=2 color=red>·对不起,您输入的邮箱地址有误!</font>";
		return false;
	}
}

function checksenderPhone(senderPhone){
	var zz = /\d{3}-\d{8}|\d{4}-\d{7}|^1[3|4|5|8][0-9]\d{8}$/
	if (zz.test(senderPhone)) {
		senderPhoneSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		senderPhoneSpan.innerHTML = "<font size=2 color=red>·请填写正确的固定电话或者手机号码!</font>";
		return false
	}
}
